package top.kaoshanji.ch06;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.*;

/**
 * 代码清单 ch06-03：ZonedTime示例(6.5节)
 * author kaoshanji
 * ^_^
 * create time 2024/12/9 15:34
 */
public class ZonedTimes {

    private static final Logger logger = LogManager.getLogger(ZonedTimes.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch06-03：ZonedTime示例(6.5节)...");

        logger.info("...指定时区日期时间...");
        ZonedDateTime apollo11launch = ZonedDateTime.of(1969, 7, 16, 9, 32, 0,
                0, ZoneId.of("America/New_York")); // 阿波罗发射时间
        System.out.println("apollo11launch: " + apollo11launch);

        logger.info("...ZonedDateTime --> Instant...");
        Instant instant = apollo11launch.toInstant();
        System.out.println("instant: " + instant);

        logger.info("...Instant --> ZonedDateTime(UTC时区)...");
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("UTC"));
        System.out.println("zonedDateTime: " + zonedDateTime);

        logger.info("...根据本地日期初始化时区日期时间...");
        ZonedDateTime skipped = ZonedDateTime.of(LocalDate.of(2013, 3, 31),
                LocalTime.of(2, 30), ZoneId.of("Europe/Berlin"));
        System.out.println("skipped: " + skipped);

        logger.info("...一小时之后的时间...");
        ZonedDateTime ambiguous = ZonedDateTime.of(LocalDate.of(2013, 10, 27), // End of daylight savings time
                LocalTime.of(2, 30), ZoneId.of("Europe/Berlin"));
        // 2013-10-27T02:30+02:00[Europe/Berlin]
        ZonedDateTime anHourLater = ambiguous.plusHours(1);
        // 2013-10-27T02:30+01:00[Europe/Berlin]
        System.out.println("ambiguous: " + ambiguous);
        System.out.println("anHourLater: " + anHourLater);

        logger.info("...一周之后时区日期时间...");
        ZonedDateTime meeting = ZonedDateTime.of(LocalDate.of(2013, 10, 31), // End of daylight savings time
                LocalTime.of(14, 30), ZoneId.of("America/Los_Angeles"));
        System.out.println("meeting: " + meeting);
        ZonedDateTime nextMeeting = meeting.plus(Period.ofDays(7));
        System.out.println("nextMeeting: " + nextMeeting);

    }

}
